package com.laoxu.student.business.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.laoxu.student.business.entity.Student;
import com.laoxu.student.business.service.StudentService;
import com.laoxu.student.business.vo.StudentVO;
import com.laoxu.student.common.constants.ResultCodeEnum;
import com.laoxu.student.common.exception.StudentException;
import com.laoxu.student.common.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 学生控制器
 * </p>
 *
 * @author Laoxu
 * @since 2020-04-04
 */
@Api(description="学生管理")
@CrossOrigin //跨域
@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private StudentService studentService;
    
    @ApiOperation("新增或修改学生")
    @PostMapping("save")
    public Result save(@ApiParam(name="student",value = "学生对象",required = true)
                       @RequestBody Student student){
        studentService.saveOrUpdate(student);
        return Result.ok().data("studentId",student.getId());
    }

    @ApiOperation("根据ID查询学生")
    @GetMapping("{id}")
    public Result get(@ApiParam(name="id",value = "学生ID",required = true)
                      @PathVariable Long id){
        Student student = studentService.getById(id);
        return Result.ok().data("item", student);
    }

    @ApiOperation("删除多个学生")
    @PostMapping("removes")
    public Result removes(@ApiParam(name="ids",value = "学生ID数组",required = true)
                          @RequestBody List<Long> ids){
        studentService.removeByIds(ids);
        return Result.ok();
    }

    @ApiOperation(value = "学生分页列表")
    @GetMapping("{page}/{limit}")
    public Result pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit){

        Page<StudentVO> pageParam = new Page<>(page, limit);

        studentService.pageQuery(pageParam);
        List<StudentVO> records = pageParam.getRecords();

        long total = pageParam.getTotal();

        return  Result.ok().data("total", total).data("rows", records);
    }

    @ApiOperation(value = "Excel批量导入")
    @PostMapping("import")
    public Result batchImport(
            @ApiParam(name = "file", value = "Excel文件", required = true)
            @RequestParam("file") MultipartFile file) {

        try{
            List<String> errorMsg = studentService.batchImport(file);
            if(errorMsg.size() == 0){
                return Result.ok().message("批量导入成功");
            }else{
                return Result.error().message("部分数据导入失败").data("errorMsgList", errorMsg);
            }

        }catch (Exception e){
            e.printStackTrace();
            //无论哪种异常，只要是在excel导入时发生的，一律用转成GuliException抛出
            throw new StudentException(ResultCodeEnum.EXCEL_DATA_IMPORT_ERROR);
        }
    }
}

